using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Skinner
{
    public partial class ImageView : Form
    {
        private ImageView()
        {
            InitializeComponent();
        }

        private StyleSheet _sheet;
        private string _selectorText;
        private string _url;

        public string URL
        {
            get { return _url; }
        }

        public ImageView(string image, string url, string selectorText, StyleSheet sheet)
        {
            InitializeComponent();
            ImageViewer.Load(image);
            ImageViewer.Size = ImageViewer.Image.Size;
            ImageViewer.SizeMode = PictureBoxSizeMode.Zoom;

            int h = ClientSize.Height;
            if (h < ImageViewer.Image.Height)
                h = ImageViewer.Image.Height + 50;

            int w = ClientSize.Width;
            if (w < ImageViewer.Image.Width)
                w = ImageViewer.Image.Width + 24;

            this.ClientSize = new Size(w, h);
            ImageViewer.Location = new Point(12, 12);
            ImageViewer.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;

            _sheet = sheet;
            _selectorText = selectorText;
            _url = url;
            this.Text = this.Text + " " + image;

            lnkStyle.Text = _sheet.Rules[_selectorText.ToUpper()].Rule.selectorText;
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                _url = string.Format("URL({0})", openFileDialog.FileName);
                ImageViewer.Load(openFileDialog.FileName);
                if (_sheet.Rules[_selectorText.ToUpper()].Rule.style.backgroundImage != null)
                {
                    _sheet.Rules[_selectorText.ToUpper()].Rule.style.backgroundImage = _url;
                }
                else
                {
                    System.Diagnostics.Debug.Print("huh");
                }
                if (!_sheet.ReplacementImages.ContainsKey(_selectorText.ToUpper()))
                    _sheet.ReplacementImages.Add(_selectorText.ToUpper(), openFileDialog.FileName);
                else
                    _sheet.ReplacementImages[_selectorText.ToUpper()] = openFileDialog.FileName;
            }

            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                ImageViewer.Image.Save(saveFileDialog.FileName);
            }
        }

        private void lnkStyle_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            StyleEdit editor = new StyleEdit(_sheet.Rules[_selectorText.ToUpper()]);
            editor.Show();
        }
    }
}